<template>
<a-card :bordered="false" :class="'cust-erp-sub-tab'">
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="mainId">
        <a-button v-if="parentRows[0].state != '通过'" @click="production" type="primary" icon="plus">新增</a-button>
        <a-button type="primary" icon="download" @click="handleExportXls('领料货品表')">导出</a-button>
        <a-upload v-if="parentRows[0].state != '通过'" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
            <a-button type="primary" icon="import">导入</a-button>
        </a-upload>
        <a-dropdown v-if="selectedRowKeys.length > 0">
            <a-menu slot="overlay">
                <a-menu-item key="1" @click="batchDel">
                    <a-icon type="delete" />删除</a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px"> 批量操作
                <a-icon type="down" />
            </a-button>
        </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
        <div v-if="parentRows[0].state != '通过'" class="ant-alert ant-alert-info" style="margin-bottom: 16px;" >
            <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
            <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>

        <a-table ref="table" size="middle" bordered rowKey="id" :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading" :scroll={x:1400} :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" @change="handleTableChange">

            <template slot="htmlSlot" slot-scope="text">
                <div v-html="text"></div>
            </template>
            <template slot="imgSlot" slot-scope="text">
                <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
                <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" />
            </template>
            <template slot="fileSlot" slot-scope="text">
                <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
                <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="uploadFile(text)">
                    下载
                </a-button>
            </template>

            <span slot="action" slot-scope="text, record">
                <!-- <a>编辑</a> -->
                <!-- <a-divider type="vertical" /> -->
                <a-popconfirm v-if="parentRows[0].state != '通过'" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                </a-popconfirm>
                <a-popconfirm v-else disabled title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a disabled>删除</a>
                </a-popconfirm>
            </span>

        </a-table>
    </div>
    <productionList @Success="Success" :columns="Prop_columns" :columnList="Prop_columnList" :otherData="Prop_otherData" :showPorductList="showPorductList" :mainId="mainId" :sendData="sendData" @hidden="hidden"></productionList>
</a-card>
</template>

<script>
import {
    mapState
} from 'vuex'
import {
    JeecgListMixin
} from '@/mixins/JeecgListMixin'
import {
    getAction,
} from '@/api/manage'
import '@assets/less/common.less'
import ProductionList from './ProductionList'

export default {
    name: "PickingSubList",
    mixins: [JeecgListMixin],
    components: {
        ProductionList
    },
    props: {
        mainId: {
            type: String,
            default: '',
        },
        parentRows: {
            type: Array,
            default: [],
        },
    },
    watch: {
        mainId: {
            immediate: true,
            handler(val) {
                if (!this.mainId) {
                    this.clearList()
                } else {
                    this.queryParam['pickingId'] = val
                    this.loadData(1);
                }
            }
        }
    },
    data() {
        return {
            sendData: [],
            showPorductList: false,
            description: '领料表管理页面',
            disableMixinCreated: true,
            // 表头
            columns: [{
                    title: '序号',
                    dataIndex: '',
                    key: 'rowIndex',
                    fixed: true,
                    width: 70,
                    align: "center",
                    customRender: function (t, r, index) {
                        return parseInt(index) + 1;
                    }
                },
                {
                    title: '货品名称',
                    align: "center",
                    fixed: true,
                    width: 160,
                    dataIndex: 'itemName'
                },
                {
                    title: '货品编码',
                    width: 160,
                    align: "center",
                    dataIndex: 'itemCode'
                },
                {
                    title: '规格',
                    align: "center",
                    dataIndex: 'specifications'
                },
                {
                    title: '单位',
                    align: "center",
                    dataIndex: 'unit'
                },
                {
                    title: '数量',
                    align: "center",
                    dataIndex: 'number'
                },
                {
                    title: '领用数量',
                    align: "center",
                    dataIndex: 'recipientsNumber'
                },
                {
                    title: '备注',
                    align: "center",
                    dataIndex: 'note'
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    align: "center",
                    width: 150,
                    fixed: 'right',
                    scopedSlots: {
                        customRender: 'action'
                    },
                }
            ],
            url: {
                list: "/production/picking/listPickingSubByMainId",
                delete: "/production/picking/deletePickingSub",
                deleteBatch: "/production/picking/deleteBatchPickingSub",
                exportXlsUrl: "/production/picking/exportPickingSub",
                importUrl: "/production/picking/importPickingSub",
            },
            dictOptions: {},

        }
    },
    computed: {
        ...mapState({
            Prop_columns: state => state.PickingSubList.columns
        }),
        ...mapState({
            Prop_columnList: state => state.PickingSubList.columnList
        }),
        ...mapState({
            Prop_otherData: state => state.PickingSubList.otherData
        }),
        importExcelUrl() {
            return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`;
        }
    },
    methods: {
        hidden(value) {
            this.showPorductList = value;
        },
        async production() {
            if (this.parentRows[0].warehouseId) {
                this.showPorductList = true;
                let result = await getAction('/stock/inventory/list', {
                    warehouse: this.parentRows[0].warehouse
                })
                this.sendData = result.result.records;
                this.sendData.forEach(elem => {
                    elem.key = elem.itemCode;
                })
            } else {
                this.$message.warning('请选择主表仓库!')
            }
        },
        clearList() {
            this.dataSource = []
            this.selectedRowKeys = []
            this.ipagination.current = 1
        },
        //添加数据成功后的回调函数
        Success() {
            this.queryParam.mainId = this.mainId;
            this.loadData()
            this.$emit('callBack')
        }
    }
}
</script>
